package com.ftx.controller.test.annotation.condition;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * Created by fengtingxin on 2020/4/10.
 */
public class LinuxCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        System.out.println("我再 LinuxCondition");
        // 1.能够获取到ioc使用的beanFactory
        BeanFactory beanFactory = context.getBeanFactory();
        // 2. 获取类加载器
        ClassLoader classLoader = context.getClassLoader();
        // 3. 获取环境
        Environment environment = context.getEnvironment();
        // 4. 获取bean定义的注册类
        BeanDefinitionRegistry beanDefinition = context.getRegistry();

        if (beanDefinition.containsBeanDefinition("kaifei")){
            System.out.println("contain kaifei !!!!!!!!!!!!!!!!!!11");
        }

        String systemName = environment.getProperty("os.name");
        if (beanDefinition.containsBeanDefinition("wangwu")){
            System.out.println("contain zhangsan !!!!!!!!!!!!!!!!!");
        }
        if (systemName.contains("linux")){
            System.out.println("os.name 包含linux");
            return true;
        }
        return false;
    }
}
